package com.scm.erp.framework.pojo;


import org.springframework.security.core.GrantedAuthority;
import org.springframework.security.core.authority.SimpleGrantedAuthority;
import org.springframework.security.core.userdetails.UserDetails;

import javax.persistence.*;
import java.io.Serializable;
import java.util.*;

/**
 * 用户表
 *   多个用户表只能有一个部门
 *   根据真实信息进行登记注册
 *
 * @param "uDepartment"
 * */
@Entity
@Table(name = "T_USER")
public class tUser implements Serializable,UserDetails {

    /**
     * 主键自增长
     */
    @Id
    @GeneratedValue(strategy=GenerationType.IDENTITY)
    @Column(name = "U_ID", nullable=false)
    private Long uId;
    /**
     * 账号
     * */
    @Column(name = "U_ACCOUNT", nullable=false)
    private String uAccount;

    /**
     * 真实姓名
     * */
    @Column(name = "U_NAME", nullable=false)
    private String uName;
    /**
     * 邮箱
     * */
    @Column(name = "U_EMAIL", nullable=false,unique = true)
    private String u_Email;
    /**
     * 身份证
     * */
    @Column(name = "U_CARD", nullable=false,unique = true)
    private String uCard;
    /**
     * 生日
     * */
    @Column(name = "U_BIRTHDAY", nullable=false)
    private String uBirthday;
    /**
     * 密码
     * */
    @Column(name = "U_PASSWORD", nullable=false)
    private String uPassword;
    /**
     * 登入日期
     * */
    @Temporal(TemporalType.TIMESTAMP)
    @Column(name = "U_DATE", nullable = false)
    private Date uDate;
    /**
     * 是否过期
     */
    @Column(name = "U_ACCOUNTNONEXPIRED", nullable = false)
    private Boolean uAccountNonExpired;

    /**
     * 账户是否锁定
     */
    @Column(name = "U_ACCOUNTNONLOCKED", nullable = false)
    private Boolean uAccountNonLocked;

    /**
     * 凭证是否过期
     */
    @Column(name = "U_CREDENTIALSNONEXPIRED", nullable = false)
    private Boolean uCredentialsNonExpired;
    /**
     * 是否启用
     */
    @Column(name = "U_ENABLED", nullable = false)
    private Boolean uEnabled;


    /**
     * 关联角色表
     */
    @ManyToMany(fetch = FetchType.EAGER,targetEntity = tRole.class,cascade = CascadeType.ALL)
    @JoinTable(name = "T_USER_AND_T_ROLE",
            joinColumns ={ @JoinColumn(name  = "U_ID")},
            inverseJoinColumns = @JoinColumn(name  = "r_ID"))
    private Set<tRole> rRoleSet;



    public Long getuId() {
        return uId;
    }

    public void setuId(Long uId) {
        this.uId = uId;
    }

    public Set<tRole> getuRoleSet() {
        return rRoleSet;
    }

    public void setuRoleSet(Set<tRole> uRoleSet) {
        this.rRoleSet = uRoleSet;
    }

    public String getuAccount() {
        return uAccount;
    }

    public void setuAccount(String uAccount) {
        this.uAccount = uAccount;
    }

    public String getuName() {
        return uName;
    }

    public void setuName(String uName) {
        this.uName = uName;
    }

    public String getU_Email() {
        return u_Email;
    }

    public void setU_Email(String u_Email) {
        this.u_Email = u_Email;
    }

    public String getuCard() {
        return uCard;
    }

    public void setuCard(String uCard) {
        this.uCard = uCard;
    }

    public String getuBirthday() {
        return uBirthday;
    }

    public void setuBirthday(String uBirthday) {
        this.uBirthday = uBirthday;
    }

    public String getuPassword() {
        return uPassword;
    }

    public void setuPassword(String uPassword) {
        this.uPassword = uPassword;
    }

    public Date getuDate() {
        return uDate;
    }

    public void setuDate(Date uDate) {
        this.uDate = uDate;
    }

    public Boolean getuAccountNonExpired() {
        return uAccountNonExpired;
    }

    public void setuAccountNonExpired(Boolean uAccountNonExpired) {
        this.uAccountNonExpired = uAccountNonExpired;
    }

    public Boolean getuAccountNonLocked() {
        return uAccountNonLocked;
    }

    public void setuAccountNonLocked(Boolean uAccountNonLocked) {
        this.uAccountNonLocked = uAccountNonLocked;
    }

    public Boolean getuCredentialsNonExpired() {
        return uCredentialsNonExpired;
    }

    public void setuCredentialsNonExpired(Boolean uCredentialsNonExpired) {
        this.uCredentialsNonExpired = uCredentialsNonExpired;
    }

    public Boolean getuEnabled() {
        return uEnabled;
    }

    public void setuEnabled(Boolean uEnabled) {
        this.uEnabled = uEnabled;
    }

    public tUser(Boolean uAccountNonExpired, Boolean uAccountNonLocked, Boolean uCredentialsNonExpired, Boolean uEnabled) {
        this.uAccountNonExpired = uAccountNonExpired;
        this.uAccountNonLocked = uAccountNonLocked;
        this.uCredentialsNonExpired = uCredentialsNonExpired;
        this.uEnabled = uEnabled;
    }

    public tUser() {
    }

    @Override
    public Collection<? extends GrantedAuthority> getAuthorities() {
        List<GrantedAuthority> auths = new ArrayList<>();
        for(tRole role : this.getuRoleSet())
        {
            for(tPermissions permissions:role.getpPermissionsSet()){
                auths.add(new SimpleGrantedAuthority(permissions.getpPermissionIdentifier()));
            }
        }
        return auths;

    }

    @Override
    public String getPassword() {
        return this.getuPassword();
    }

    @Override
    public String getUsername() {
        return String.valueOf(this.getuAccount());
    }

    @Override
    public boolean isAccountNonExpired() {
        return this.getuAccountNonExpired();
    }

    @Override
    public boolean isAccountNonLocked() {
        return this.getuAccountNonLocked();
    }

    @Override
    public boolean isCredentialsNonExpired() {
        return this.getuCredentialsNonExpired();
    }

    @Override
    public boolean isEnabled() {
        return this.getuEnabled();
    }

    @Override
    public String toString() {
        return "tUser{" +
                "uId=" + uId +
                ", uAccount='" + uAccount + '\'' +
                ", uName='" + uName + '\'' +
                ", u_Email='" + u_Email + '\'' +
                ", uCard='" + uCard + '\'' +
                ", uBirthday=" + uBirthday +
                ", uPassword='" + uPassword + '\'' +
                ", uDate=" + uDate +
                ", uAccountNonExpired=" + uAccountNonExpired +
                ", uAccountNonLocked=" + uAccountNonLocked +
                ", uCredentialsNonExpired=" + uCredentialsNonExpired +
                ", uEnabled=" + uEnabled +
                '}';
    }
}
